<?php

namespace app\common\model;

use app\common\model\BaseModel;


class Notice extends BaseModel
{
    // 表名
    protected $name = 'notice';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'create_time_text'
    ];
    

    
    public function getTypeList()
    {
        return ['0' => __('用户端'), '1' => __('司机端')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id')->joinType('LEFT');
    }

    public static function record($user_id,$title,$content,$user_type=0,$order_id=0,$sub_order_id=0){
        $notice=[
            'user_id'=>$user_id,
            'title'=>$title,
            'content'=>$content,
            'type'=>$user_type,
            'order_id'=>$order_id,
            'sub_order_id'=>$sub_order_id,
            'create_time'=>time()
        ];
        self::insert($notice);
    }
}
